class Solution(object):
    def minimumEffort(self, tasks):
        tasks.sort(key = lambda x: (x[1] - x[0], x[1]), reverse = True)
        ans = tasks[0][1]
        remain = ans - tasks[0][0]
        n = len(tasks)
        for i in range(1, n):
            if tasks[i][1] > remain:
                ans += tasks[i][1] - remain
                remain = tasks[i][1] - tasks[i][0]
            else:
                remain -= tasks[i][0]
        return ans




data = Solution()
tasks = [[1,2],[2,4],[4,8]]
print(data.minimumEffort(tasks))

